package cn.edu.xjtu.data_structure.chapter01_linearlist.question;

/**
 * @author yjq
 * @version 1.0
 * @date 2021/9/10 16:01
 */
public class ReverseTest {
    //将一长度为n的数组循环左移p个位置
    void Reverse(int[] a, int left, int right, int k) {
        int temp;
        for (int i = left, j = right; i < left + k && i < j; i++, j--) {
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }

    public static void main(String[] args) {
        int[] a = new int[]{1,2,3,4,5,6,7,8,9};
        System.out.print("before reverse: ");
        for (int i = 0; i <9 ; i++) {
            System.out.print(a[i]+" ");
        }
        System.out.println();
        ReverseTest  test = new ReverseTest();
        //循环左移三位
        test.Reverse(a,0,2,3);
        test.Reverse(a,3,8,6);
        test.Reverse(a,0,8,9);
        System.out.print("after reverse: ");
        for (int i = 0; i <9 ; i++) {
            System.out.print(a[i]+" ");
        }
    }
}
